home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / NextAnswers / 1222_view_and_window_resizing.rtf < prev    next >
Text File  |  1995-06-12  |  5KB  |  126 lines

  1. {\rtf0\ansi{\fonttbl\f0\fnil Times-Roman;\f2\fmodern Ohlfs;}
  2. \paperw12140
  3. \paperh11540
  4. \margl120
  5. \margr120
  6. {\colortbl;\red0\green0\blue0;\red79\green79\blue79;}
  7. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\b0\i0\ulnone\fs28\fc1\cf1 Q:  How do I make a window resize from a corner other than the upper right?\
  8. \
  9. Q:  I have a view that I want to resize. However, what I really want is for the window to be resized such that my view becomes the given size using the autosizing attributes that I have set in my window. How do I do that?\
  10. \
  11. A:  The following window method, sizeWindow:byCorner:, resizes a window to the given size by moving the given corner. The view method below it, sizeTo::byWindowCorner:, resizes a view by growing its window such that the view becomes the given size with respect to its autosizing settings. The two methods are implemented as categories to Window and View respectively.\
  12. \
  13.  
  14. \pard\tx540\tx1080\tx1620\tx2160\tx2700\tx3240\tx3780\tx4320\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f2\fs20\fc1\cf1 // Format is 80 columns with 4 space tabs.\
  15. @interface Window(Sizing)\
  16. - sizeWindow:(NXCoord)width :(NXCoord)height byCorner:(int)corner\
  17. @end\
  18. \
  19. @implementation Window(Sizing)\
  20. \
  21. #define CORNER_UPPER_LEFT    0\
  22. #define CORNER_LOWER_LEFT    1\
  23. #define CORNER_UPPER_RIGHT    2\
  24. #define CORNER_LOWER_RIGHT    3\
  25. \
  26. // Keep 'a' between x and y\
  27. #define CLAMP(a,x,y) (MAX((x), MIN((y), (a))))\
  28. \
  29. /******************************************************************************\
  30.     This Method resizes the receiving window as if it was dragged with the given corner. This method is useful when you want the window to resize by a corner other that the default upper right.\
  31. ******************************************************************************/\
  32. - sizeWindow:(NXCoord)width :(NXCoord)height byCorner:(int)corner\
  33. \{\
  34.     NXRect newFrame;\
  35.     NXSize minSize, maxSize;\
  36. \
  37.     // Clamp width and height to their respective minimum and maximum values\
  38.     [self getMinSize:&minSize]; [self getMaxSize:&maxSize];\
  39.     width = CLAMP(width, minSize.width, maxSize.width);\
  40.     height = CLAMP(height, minSize.height, maxSize.height);\
  41. \
  42.     // Set newFrame from the old frame and the new sizes\
  43.     NXSetRect(&newFrame, NX_X(&frame), NX_Y(&frame), width, height);\
  44. \
  45.     // Move the respective corner by the amount of growth and set newFrame\
  46.     switch(corner) \{\
  47.         case CORNER_UPPER_LEFT:\
  48.             NX_X(&newFrame) -= width - NX_WIDTH(&frame);\
  49.             break;\
  50.         case CORNER_LOWER_LEFT:\
  51.             NX_X(&newFrame) -= width - NX_WIDTH(&frame);\
  52.             NX_Y(&newFrame) -= height - NX_HEIGHT(&frame);\
  53.             break;\
  54.         case CORNER_UPPER_RIGHT:\
  55.             break;\
  56.         case CORNER_LOWER_RIGHT:\
  57.             NX_Y(&newFrame) -= height - NX_HEIGHT(&frame);\
  58.             break;\
  59.     \}\
  60.     [self placeWindowAndDisplay:&newFrame];\
  61.     return self;\
  62. \}\
  63. @end\
  64. \
  65. \
  66. @interface View(Sizing)\
  67. - sizeTo:(NXCoord)width :(NXCoord)height byWindowCorner:(int)corner;\
  68. @end\
  69. \
  70. @implementation View(Sizing)\
  71. \
  72. /******************************************************************************\
  73.     This Method resizes the receiving view to the given width and height by resizing the window by the appropriate amount with respect to autosizing. This method is useful for those occasions when you know what size a view should be, but don't know how big to make the window to hold it. If you ask for a new width, it assumes the view is width sizable. The same goes for height. If the hierarchy contains a ClipView (ie, in a ScrollView) it assumes that you want the ClipView's subview to be fully exposed. The window size will not exceed the set maximum.\
  74. ******************************************************************************/\
  75. - sizeTo:(NXCoord)width :(NXCoord)height byWindowCorner:(int)corner\
  76. \{\
  77.     int autosizing = [self autosizing];\
  78.     float widthGrowth = width - NX_WIDTH(&bounds);\
  79.     float heightGrowth = height - NX_HEIGHT(&bounds);\
  80.     float stretchingWidth = NX_WIDTH(&bounds);\
  81.     float stretchingHeight = NX_HEIGHT(&bounds);\
  82.     float newSuperWidth, newSuperHeight;\
  83.     NXRect superFrame;\
  84. \
  85.     // If we are a contentView we simply need to grow window by our growth\
  86.     if(self==[window contentView]) \{\
  87.         [window getFrame:&superFrame];\
  88.         [window sizeWindow:NX_WIDTH(&superFrame) + widthGrowth\
  89.             :NX_HEIGHT(&superFrame) + heightGrowth byCorner:corner];\
  90.     \}\
  91.     else \{\
  92.         [superview getFrame:&superFrame];\
  93. \
  94.         // Add margins to stretching lengths if they have been turned on in IB\
  95.         if(autosizing & NX_MINXMARGINSIZABLE) stretchingWidth += NX_X(&frame);\
  96.         if(autosizing & NX_MAXXMARGINSIZABLE)\
  97.             stretchingWidth += NX_WIDTH(&superFrame) - NX_MAXX(&frame);\
  98.         if(autosizing & NX_MINYMARGINSIZABLE)\
  99.             stretchingHeight += NX_Y(&frame);\
  100.         if(autosizing & NX_MAXYMARGINSIZABLE)\
  101.             stretchingHeight += NX_HEIGHT(&superFrame) - NX_MAXY(&frame);\
  102. \
  103.         // Add growth times a ratio of stetching length::view length to Super\
  104.         newSuperWidth = NX_WIDTH(&superFrame) +\
  105.             widthGrowth*stretchingWidth/NX_WIDTH(&bounds);\
  106.         newSuperHeight = NX_HEIGHT(&superFrame) +\
  107.             heightGrowth*stretchingHeight/NX_HEIGHT(&bounds);\
  108. \
  109.         // Resize the Superview\
  110.         [superview sizeTo:newSuperWidth :newSuperHeight \
  111.             byWindowCorner:corner];\
  112.         \
  113.         // If we are a ClipView, bring the docview up to size\
  114.         if([self isKindOf:[ClipView class]]) \
  115.             [[(ClipView *)self docView] sizeTo:width :height];\
  116.     \}\
  117.     return self;\
  118. \}\
  119.  
  120. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\fs28\fc1\cf1 \
  121. QA886\
  122. \
  123. Valid for 1.0, 2.0, 3.0, 3.1\
  124. \
  125.  
  126.